package jsyf.oa.dao.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.device.DeviceRepaymentPlanEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 92852 on 2018/4/25.
 */
@Repository
public class DeviceRepaymentPlanDao extends BaseDao {
    public DeviceRepaymentPlanDao() {
        super.iBaseEntity = new DeviceRepaymentPlanEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, DeviceRepaymentPlanEntity.class);
    }
    public List<JSONObject> getRemindReplaymentList(){
        StringBuffer sql=new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_repayment_plan.ID as id,");
        sql.append(" t_dvc_repayment_plan.EXPIRATION_DATE as expirationDate,");
        sql.append(" t_dvc_repayment_plan.EXPIRATION_MONEY as expirationMoney,");
        sql.append(" t_dvc_repayment_plan.PAYMENT_TYPE as paymentType,");
        sql.append(" t_dvc_repayment_plan.PAYEE as payee,");
        sql.append(" t_dvc_repayment_plan.PAYEE_ACCOUNT as payeeAccount,");
        sql.append(" t_dvc_repayment_plan.PAYMENT_MONEY as paymentMoney,");
        sql.append(" t_dvc_repayment_plan.PAYMENT_DATE as paymentDate,");
        sql.append(" t_dvc_repayment_plan.SAMPLE_ID as sampleId,");
        sql.append(" t_dvc_sample.SEQUENCE as sequence");
        sql.append(" FROM");
        sql.append(" t_dvc_repayment_plan LEFT JOIN t_dvc_sample ON t_dvc_repayment_plan.SAMPLE_ID=t_dvc_sample.ID");
        sql.append(" WHERE");
        sql.append(" DATEDIFF(now(), EXPIRATION_DATE) BETWEEN 0 AND 5 AND t_dvc_repayment_plan.PAYMENT_MONEY<t_dvc_repayment_plan.EXPIRATION_MONEY");
        return  super.queryForJsonList(sql.toString());
    }
    @Override
    public Integer removeByCondition(String condition, String[] params) {
        String mainId = params[0];
        if (null != params && params.length > 0 && null != mainId) {
            StringBuffer sql = new StringBuffer();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = super.getCurrentTime();
            String dateFormat = format.format(date);
            sql.append(" UPDATE " + iBaseEntity.getTableName() + " SET LOG_FLAG=1 \n" +
                    " , UPDATE_TIME = str_to_date('" + dateFormat + "', '%Y-%m-%d %H:%i:%s')\n" +
                    " WHERE 1 = 1 \n" +
                    " AND LOG_FLAG=0 \n");
            sql.append(condition);
            this.getJdbcTemplate().update(sql.toString(), params);
            return 1;
        } else {
            return 0;
        }
    }

}
